import os
import time
from apps.contents.models import ContentCategory
from utlis.goods import get_categories
from django.template import loader
from meiduo_mall import settings


def generic_meiduo_index():
    print('************%s*************' % time.ctime())

    # 商品分类
    categories = get_categories()
    # 广告数据
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 组织模板数据
    context = {
        'categories': categories,
        'contents': contents,
    }
    # 获取首页模板文件
    index_template = loader.get_template('index.html')
    # 渲染首页模板
    index_data = index_template.render(context)
    # 指定写入文件路径
    file = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')

    with open(file, 'w', encoding='utf-8')as f:
        f.write(index_data)
